package test4_3.demo02;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class MainApp {
    private Trader raoul = new Trader("Raoul", "Cambridge");
    private Trader mario = new Trader("Mario", "Milan");
    private Trader alan = new Trader("Alan", "Cambridge");
    private Trader brian = new Trader("Brian", "Cambridge");

    public List<Transaction> getTransactionList() {
        return transactionList;
    }

    private List<Transaction> transactionList = Arrays.asList(
            new Transaction(brian, 2011, 300),
            new Transaction(raoul, 2012, 1000),
            new Transaction(raoul, 2011, 400),
            new Transaction(mario, 2012, 710),
            new Transaction(mario, 2012, 700),
            new Transaction(alan, 2012, 950)
    );

    public static void main(String[] args) {
        MainApp mainApp = new MainApp();
        //1.找出2011年发生的所有交易，并按交易额排序（从高到低）。
        List<Transaction> str2011 = mainApp.getTransactionList().stream()
                .filter(n -> n.getYear()==2011)
                .collect(Collectors.toList());
        Collections.sort(str2011, (a, b) -> b.getValue() - a.getValue());
        str2011.forEach(System.out::println);
        System.out.println("==================");

        //2.交易员都在哪些不同的城市工作过？
        List<Trader> traders = Arrays.asList(mainApp.raoul, mainApp.mario, mainApp.alan, mainApp.brian);
        List<String> strCities = traders.stream()
                .map(Trader::getCity)
                .distinct()
                .collect(Collectors.toList());
        strCities.forEach(System.out::println);
        System.out.println("==================");


        //3.查找所有来自Cambridge的交易员，并按姓名排序。
        List<String> strCitiesCambridge = traders.stream()
                .filter(n -> n.getCity().equals("Cambridge"))
                .map(Trader::getName)
                .collect(Collectors.toList());
        Collections.sort(strCitiesCambridge, (a, b) -> a.length() - b.length());
        strCitiesCambridge.forEach(System.out::println);
        System.out.println("==================");

        //4.返回所有交易员的姓名字符串，按字母顺序排序。
        List<String> strTraderName = traders.stream()
                .map(Trader::getName)
                .collect(Collectors.toList());
        Collections.sort(strTraderName);
        strTraderName.forEach(System.out::println);
        System.out.println("==================");

        //5.有没有交易员在Milan工作
        List<String> strCitiesIsMilan = traders.stream()
                .filter(n -> n.getCity().equals("Milan"))
                .map(Trader::getName)
                .collect(Collectors.toList());
        if (strCitiesIsMilan.isEmpty()){
            System.out.println("没有交易员在Milan工作");
        }else {
            System.out.println("有交易员在Milan工作");
        }
        strCitiesIsMilan.forEach(System.out::println);
        System.out.println("==================");

        //6.打印生活在Cambridge的交易员的所有交易额。
        List<Transaction> cambridgeTransactions = mainApp.transactionList.stream()
                .filter(t -> "Cambridge".equals(t.getTrader().getCity()))
                .collect(Collectors.toList());
        cambridgeTransactions.forEach(System.out::println);

        List<Integer> transactionMoney = cambridgeTransactions.stream()
                .map(Transaction::getValue)
                .collect(Collectors.toList());
        transactionMoney.forEach(System.out::println);
        System.out.println("==================");

        //所有的交易额中，最高的交易额是多少？
        int maxValue = mainApp.getTransactionList().stream()
                .mapToInt(Transaction::getValue) // 提取交易额
                .max() // 获取最大值
                .orElse(0); // 如果流为空，返回默认值 0

        System.out.println("最高交易额: " + maxValue);

        //8.所有的交易额中，找到交易额最小的交易
        Transaction minTransaction = mainApp.getTransactionList().stream()
                .reduce((t1, t2) -> t1.getValue() < t2.getValue() ? t1 : t2)
                .orElse(null);
        if (minTransaction != null) {
            System.out.println("交易额最小的交易: " + minTransaction);
        } else {
            System.out.println("交易列表为空");
        }


    }
}
